//
//  chaxunbenjiViewController.m
//  PASystem
//
//  Created by michael on 16/9/26.
//  Copyright © 2016年 czd. All rights reserved.
//
#import "zySheetPickerView.h"
#import "chaxunbenjiTableViewController.h"
#import "chaxunzhihuiTableViewController.h"
#import "chaxunjichuTableViewController.h"
#import "chaxunbenjiViewController.h"
#import "MHDatePicker.h"
#import "selectupboxView.h"
@interface chaxunbenjiViewController ()
@property (strong, nonatomic)  UITextView *nameField;
@property (strong, nonatomic)  UIButton *button1;
@property (strong, nonatomic)  UIButton *button2;
@property (strong, nonatomic) MHDatePicker *selectDatePicker;
@property (nonatomic, copy)NSString * timebeforestring;
@property (nonatomic, copy)NSString * timeafterstring;
@property (strong, nonatomic)selectupboxView  * selectview2;
@property (nonatomic,strong)UIButton *btn;

@property (nonatomic,strong)UIButton *btn0;
@property (nonatomic,assign) NSInteger btnClick;

@property (nonatomic,assign) NSInteger btnClick1;

@property (nonatomic,strong) UIButton *lastBtn0;
@property (nonatomic,strong) UIButton *lastBtn;
@property (nonatomic,strong) UIButton *lastBtn1;
//点击按钮 弹出的view

@property (nonatomic,strong) UIView *middleView;

@end

@implementation chaxunbenjiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _benjistring = @"全部单位";
    _type = @"省本级会员";
    self.view.backgroundColor = [UIColor whiteColor];
self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth,88 ) style:UITableViewStylePlain];
_tableview.dataSource = self;
_tableview.delegate = self;
[self.view addSubview:_tableview];
 //点击手势
 UITapGestureRecognizer * tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keybordDown)];
    [self.view addGestureRecognizer:tap];
UIButton *rightButton = [UIButton buttonWithType:UIButtonTypeCustom];
rightButton.frame = CGRectMake(0, 0, 44, 44);
[rightButton addTarget:self action:@selector(rightButtonAction:) forControlEvents:UIControlEventTouchUpInside];
[rightButton setTitle:@"查询" forState:UIControlStateNormal];
[rightButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
                 
        self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];

[ self initview];
}
- (void)rightButtonAction:(UIButton *)button {
if ([_type isEqualToString:@"省本级会员"]){

chaxunbenjiTableViewController *vc = [[chaxunbenjiTableViewController alloc]init];


vc.timeafterstring = _timeafterstring;
vc.timebeforestring = _timebeforestring;
vc.string = _type;
vc.name = _benjistring;
vc.namestring = _nameField.text;



[self.navigationController pushViewController:vc animated:YES];

}
if ([_type isEqualToString:@"支会企业会员"]){

chaxunzhihuiTableViewController *vc = [[chaxunzhihuiTableViewController alloc]init];


vc.timeafterstring = _timeafterstring;
vc.timebeforestring = _timebeforestring;
vc.string = _type;
vc.name = _place;
vc.namestring = _nameField.text;



[self.navigationController pushViewController:vc animated:YES];

}
if ([_type isEqualToString:@"企业基础库"]){

chaxunjichuTableViewController *vc = [[chaxunjichuTableViewController alloc]init];


vc.timeafterstring = _timeafterstring;
vc.timebeforestring = _timebeforestring;
vc.string = _type;
vc.name = _benjistring;
vc.namestring = _nameField.text;



[self.navigationController pushViewController:vc animated:YES];

}




}
- (void)initview{

// _selectview2 = [selectupboxView checkBoxFootView];
//      _selectview2.frame = CGRectMake(0, 88, KScreenWidth, 40);
//[self.view addSubview:_selectview2];

NSArray *array = @[@"省本级会员",@"支会企业会员",@"企业基础库"];
float itemWidth = (KScreenWidth) /array.count;

for (int i = 0; i<array.count; i++) {

_btn = [UIButton buttonWithType:UIButtonTypeCustom];
    _btn.frame  = CGRectMake(itemWidth*i,_tableview.bottom, itemWidth, 40);
    if (i == 0){

       _btn.selected = YES;
       _lastBtn0 = _btn;
       

}
else{
_btn.selected = NO;
}
        [_btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];

[_btn setTitle:array[i] forState:UIControlStateNormal];
_btn.tag = i;
     [_btn setTitleColor:[UIColor blueColor ] forState:UIControlStateSelected];
_btn.titleLabel.font = [UIFont systemFontOfSize: 14.0];
_btn.backgroundColor= [UIColor whiteColor];

//        _btn.layer.cornerRadius = 10;
//        [_btn.layer setBorderColor:[UIColor whiteColor].CGColor];
//        [_btn.layer setBorderWidth:2];
//        _btn.layer.masksToBounds = YES;
_btn.tag = i;
[_btn addTarget:self action:@selector(action:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:_btn];
//[_scrollView addSubview:_btn];

}

 _view0 = [[UIView alloc]initWithFrame:CGRectMake(0, _btn.bottom, KScreenWidth, 40)];
   _view0.backgroundColor = [UIColor whiteColor];
   _view0.tag = 11;
   [self.view addSubview:_view0];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, 40)];
    label.text = @"会员类型 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [_view0 addSubview:label];
     _btn0 = [UIButton buttonWithType:1];
    _btn0.frame  = CGRectMake(label.right,0, KScreenWidth-110, label.height-5);
    NSString *string = [NSString stringWithFormat:@"%@",_benjistring];
    [_btn0 setTitle:@"全部单位" forState:UIControlStateNormal];
    [_view0 addSubview:_btn0];
     UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth-50, 0, 30, 30)];
    image.image = [UIImage imageNamed:@"ios下拉"];
    [_view0 addSubview:image];
    [_btn0 addTarget:self action:@selector(clickIt) forControlEvents:UIControlEventTouchUpInside];







}
- (void)action:(UIButton *)btn{
for (UIView *view in self.view.subviews) {
    if (view.tag ==11) {
    [view removeFromSuperview];
    
}}
        [btn.titleLabel setTextColor:[UIColor blueColor]];
   btn.selected = YES;
   if (self.lastBtn0 == btn) {

        [btn.titleLabel setTextColor:[UIColor blueColor]];
        self.btnClick ++;
        
    }else {

        [self.lastBtn0.titleLabel setTextColor:[UIColor blackColor]];
        
        self.btnClick = 0;
    }
    
   self.lastBtn0 = btn;
   
   
   if (btn.tag==0) {
   
   _view0 = [[UIView alloc]initWithFrame:CGRectMake(10, _btn.bottom, KScreenWidth, 40)];
   _view0.backgroundColor = [UIColor whiteColor];
   _view0.tag = 11;
   [self.view addSubview:_view0];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
    label.text = @"会员类型 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [_view0 addSubview:label];
     _btn0 = [UIButton buttonWithType:1];
    _btn0.frame  = CGRectMake(label.right,0, KScreenWidth-110, label.height-5);
    [_btn0 setTitle:_benjistring forState:UIControlStateNormal];
    [_view0 addSubview:_btn0];
    UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth-50, 10, 30, 30)];
    image.image = [UIImage imageNamed:@"ios下拉"];
    [_view0 addSubview:image];
    [_btn0 addTarget:self action:@selector(clickIt) forControlEvents:UIControlEventTouchUpInside];


}
      if (btn.tag==1) {
      _type = @"支会企业会员";
      _view1 = [[UIView alloc]initWithFrame:CGRectMake(0, _btn.bottom, KScreenWidth, self.view.height-128)];
//      _view1.backgroundColor = [UIColor yellowColor];
      _view1.tag = 11;
      [self.view addSubview:_view1];
      
       _view2 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 150)];
      [_view1 addSubview:_view2];
    
    CGFloat W = (self.view.width - 50) / 4;
    
    CGFloat H = 30;
    
    NSArray *arr = @[@"宁波",@"温州",@"绍兴",@"湖州",@"嘉兴",@"金华",@"衢州",@"舟山",@"台州",@"丽水"];
    
    for (int i = 0 ; i < arr.count; i ++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        btn.layer.cornerRadius = 15;
        [btn.layer setBorderColor:[UIColor orangeColor].CGColor];
        [btn.layer setBorderWidth:1];
        btn.layer.masksToBounds = YES;
        
        btn.tag = i + 1;
        
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        
        btn.backgroundColor = [UIColor whiteColor];
        
        
        if (i >= 0 && i < 4) {
            btn.frame = CGRectMake(10 *(i + 1) + W*i, 15, W, H);

        }else if (i > 3 && i < 8) {
            
            btn.frame = CGRectMake(10 *(i -3) + W*(i-4), 60, W, H);
            
        }else{
            
            btn.frame = CGRectMake(10 *(i -7) + W*(i-8), 105, W, H);
            
        }
        
        [_view2 addSubview:btn];
        
   
    
}
      
      
      
      
      
      
      }
   
  else{

      _type = @"企业基础库";

}
   
}

//按钮点击---弹出一个框
- (void)btnClick:(UIButton *)btn{
    NSArray *arr = @[@"宁波",@"温州",@"绍兴",@"湖州",@"嘉兴",@"金华",@"衢州",@"舟山",@"台州",@"丽水"];

_place = arr[btn.tag - 1];
    btn.backgroundColor = [UIColor orangeColor];
    [btn.titleLabel setTextColor:[UIColor whiteColor]];
//记录按钮点击次数
    if (self.lastBtn == btn) {
        btn.backgroundColor = [UIColor orangeColor];
        [btn.titleLabel setTextColor:[UIColor whiteColor]];

        self.btnClick ++;
        
    }else {
        
        [self.lastBtn setBackgroundColor:[UIColor whiteColor]];
        [self.lastBtn.titleLabel setTextColor:[UIColor blackColor]];
        
        [self.middleView removeFromSuperview];
        
        self.btnClick = 0;
    }
    
//点击同一个按钮  移除middleView
    if (self.lastBtn == btn && (self.btnClick % 2 != 0)) {
        
        
        [self.middleView removeFromSuperview];
        
        return;
        
    }
    
    NSArray *arr1 = @[@"海曙区",@"江东",@"江北",@"北仑",@"镇海",@"鄞州",@"慈溪",@"余姚",@"奉化",@"宁海",@"象山"];
    
    NSArray *arr2 = @[@"鹿城",@"瓯海",@"龙湾",@"乐清",@"瑞安",@"永嘉",@"洞头",@"平阳",@"泰顺",@"苍南",@"文成"];
    
    NSArray *arr3 = @[@"越城",@"柯桥",@"上虞",@"诸暨",@"嵊州",@"新昌"];
    
    NSArray *arr4 = @[@"吴兴",@"南浔",@"德清",@"安吉",@"长兴"];
    
    NSArray *arr5 = @[@"南湖",@"秀洲",@"平湖",@"海宁",@"桐乡",@"嘉善",@"海盐"];
    
    NSArray *arr6 = @[@"婺城",@"金东",@"兰溪",@"东阳",@"义务",@"永康",@"浦江",@"武义",@"磐安"];

    NSArray *arr7 = @[@"柯城",@"衢江",@"江北",@"龙游",@"常山",@"开化"];
    
    NSArray *arr8 = @[@"定海",@"普陀",@"岱山",@"嵊泗"];
    
    NSArray *arr9 = @[@"椒江",@"黄岩",@"路桥",@"温岭",@"临海",@"玉环",@"三门",@"天台",@"仙居"];
    
    NSArray *arr10 = @[@"莲都",@"龙泉",@"青田",@"云和",@"庆元",@"缙云",@"遂昌",@"松阳",@"景宁"];
    
    
    UIView *middleView = [[UIView alloc] init];
    middleView.tag = 11;

    self.middleView = middleView;
    
//    middleView.backgroundColor = [UIColor colorWithRed:236/255.0 green:247/255.0 blue:250/255.0 alpha:1];
    
    [self.view addSubview:middleView];
    
    [middleView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(_view2.mas_bottom);
        
        make.left.equalTo(self.view.mas_left);
        
        make.right.equalTo(self.view.mas_right);
        
        make.height.equalTo(@100);
    }];
    
    self.lastBtn = btn;
    
    if (btn.tag == 1) {
        [self setButtonCount:arr1];
    }else if(btn.tag == 2){
        [self setButtonCount:arr2];

    
    }else if(btn.tag == 3){
        [self setButtonCount:arr3];

        
    }else if(btn.tag == 4){
        [self setButtonCount:arr4];

        
    }else if(btn.tag == 5){
        [self setButtonCount:arr5];

        
    }else if(btn.tag == 6){
        [self setButtonCount:arr6];

        
    }else if(btn.tag == 7){
        [self setButtonCount:arr7];

        
    }else if(btn.tag == 8){
        
        [self setButtonCount:arr8];

    }else if(btn.tag == 9){
        [self setButtonCount:arr9];

        
    }else if(btn.tag == 10){
        
        [self setButtonCount:arr10];

    }
    
}

//设置页面按钮个数
- (void)setButtonCount:(NSArray *)arr{
    
    CGFloat ViewW = self.view.frame.size.width;
    CGFloat w = 0;//保存前一个button的宽以及前一个button距离屏幕边缘的距离
    CGFloat h = 5;//用来控制button距离父视图的高
    for (int i = 0; i < arr.count; i ++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
//设置边框
        btn.layer.cornerRadius = 10;
        [btn.layer setBorderColor:[UIColor orangeColor].CGColor];
        [btn.layer setBorderWidth:1];
        btn.layer.masksToBounds = YES;
        [btn setTitle:arr[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [btn.titleLabel setFont:[UIFont systemFontOfSize:12]];
        
//添加行为
        [btn addTarget:self action:@selector(middBtn:) forControlEvents:UIControlEventTouchUpInside];
        //计算大小
        NSDictionary *attributes = @{NSFontAttributeName:[UIFont systemFontOfSize:10]};
        CGFloat length = [arr[i] boundingRectWithSize:CGSizeMake(ViewW, 200) options:NSStringDrawingUsesLineFragmentOrigin attributes:attributes context:nil].size.width;
        
        btn.frame = CGRectMake(10 + w, h, length + 40 , 20);
        //当button的位置超出屏幕边缘时换行 320 只是button所在父视图的宽度
        if(10 + w + length + 40 > ViewW){
            w = 0; //换行时将w置为0
            h = h + btn.frame.size.height + 10;//距离父视图也变化
            btn.frame = CGRectMake(10 + w, h, length + 40, 20);//重设button的frame
            btn.tag = 11;
        }
        w = btn.frame.size.width + btn.frame.origin.x;

        [_middleView addSubview:btn];
        
    }

}

//点击按钮  列表数据改变
- (void)middBtn:(UIButton *)btn{

 btn.backgroundColor = [UIColor orangeColor];
    [btn.titleLabel setTextColor:[UIColor whiteColor]];
//记录按钮点击次数
    if (self.lastBtn1 == btn) {
        btn.backgroundColor = [UIColor orangeColor];
        [btn.titleLabel setTextColor:[UIColor whiteColor]];

        self.btnClick1 ++;
        
    }else {
        
        [self.lastBtn1 setBackgroundColor:[UIColor whiteColor]];
        [self.lastBtn1.titleLabel setTextColor:[UIColor blackColor]];
        
        
        self.btnClick1 = 0;
    }

    self.lastBtn1 = btn;


_place =btn.titleLabel.text;
NSLog(@"%@",btn.titleLabel.text);
}




-(void)clickIt
{
    NSArray * str  = @[@"全部单位",@"副会长单位",@"常务理事单位",@"理事单位",@"普通会员",@"潜在会员"];
    zySheetPickerView *pickerView = [zySheetPickerView ZYSheetStringPickerWithTitle:str andHeadTitle:@"会员类型" Andcall:^(zySheetPickerView *pickerView, NSString *choiceString) {
    _benjistring = choiceString;
    [_btn0 setTitle:_benjistring forState:UIControlStateNormal];
            [pickerView dismissPicker];
    }];
    [pickerView show];
}
- (void)viewWillAppear:(BOOL)animated{

self.tableview.bounces = NO;

}
- (void)keybordDown {
       [self.view endEditing:YES];
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
                return 2;
 }

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
if (indexPath.row==0) {

     UITableViewCell* cell = [[UITableViewCell alloc]init];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
    label.text = @"企业名称 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
      _nameField = [[UITextView alloc]init];
      _nameField.frame = CGRectMake(label.right, label.top+5, KScreenWidth-120, cell.height-10);
//      UIColor *myColor = [UIColor colorWithWhite:0.5 alpha:0.2];
//      _nameField.backgroundColor = myColor;
 [_nameField setFont:[UIFont systemFontOfSize:13]];
 _nameField.textAlignment = NSTextAlignmentLeft;
      _nameField.delegate = self;
      _nameField.layer.borderColor = UIColor.blackColor.CGColor;_nameField.layer.borderWidth = 1;
       [cell.contentView addSubview:_nameField];
             return cell;

}
else{

     UITableViewCell* cell = [[UITableViewCell alloc]init];
      UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 100, cell.contentView.height)];
      label.numberOfLines = 0;
    label.text = @"起止时间 :";
    
    label.font = [UIFont systemFontOfSize:14];
    [cell.contentView addSubview:label];
    _button1 = [UIButton buttonWithType:UIButtonTypeCustom];
      _button1.frame = CGRectMake(label.right, label.top, 100, label.height);
//    _button1.backgroundColor = [UIColor yellowColor];
      [_button1 setTitle:@"开始时间" forState:UIControlStateNormal];
    _button1.titleLabel.font = [UIFont systemFontOfSize: 12.0];
    
       _button2 = [UIButton buttonWithType:UIButtonTypeCustom];
      _button2.frame = CGRectMake(_button1.right, label.top, 110, label.height);
      [_button2 setTitle:@"结束时间" forState:UIControlStateNormal];
          _button2.titleLabel.font = [UIFont systemFontOfSize: 12.0];
      [_button1 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
      [_button2 setTitleColor:[UIColor blackColor]forState:UIControlStateNormal];
      [_button1 addTarget:self action:@selector(action1) forControlEvents:UIControlEventTouchUpInside];
[_button2 addTarget:self action:@selector(action2) forControlEvents:UIControlEventTouchUpInside];
       [cell.contentView addSubview:_button1];
       [cell.contentView addSubview:_button2];
             return cell;



}



}
- (NSString *)dateStringWithDate:(NSDate *)date DateFormat:(NSString *)dateFormat
{
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:dateFormat];
    [dateFormatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_CN"]];
    NSString *str = [dateFormatter stringFromDate:date];
    return str ? str : @"";
}
- (void)action1{
 _selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
//        NSString *string = [NSString stringWithFormat:@"%@",[NSDate dateWithTimeInterval:3600*8 sinceDate:selectedDate]];
//        weakSelf.myLabel2.text = string;
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy-MM-dd"];
      _timebeforestring = string;
      [weakSelf.button1 setTitle:string forState:UIControlStateNormal];
    }];

}
- (void)action2{
_selectDatePicker = [[MHDatePicker alloc] init];
    _selectDatePicker.isBeforeTime = YES;
    _selectDatePicker.datePickerMode = UIDatePickerModeDate;
    
    __weak typeof(self) weakSelf = self;
    [_selectDatePicker didFinishSelectedDate:^(NSDate *selectedDate) {
//        NSString *string = [NSString stringWithFormat:@"%@",[NSDate dateWithTimeInterval:3600*8 sinceDate:selectedDate]];
//        weakSelf.myLabel2.text = string;
      NSString *string = [weakSelf dateStringWithDate:selectedDate DateFormat:@"yyyy-MM-dd"];
        _timeafterstring = string;
      [weakSelf.button2 setTitle:[NSString stringWithFormat:@"%@",string] forState:UIControlStateNormal];
    }];

}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
